package com.qianfeng.yime.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.yime.R;
import com.qianfeng.yime.model.Job;
import com.qianfeng.yime.util.BitmapHelper;
import com.qianfeng.yime.util.BitmapUtilsHelper;

import java.util.List;

/**
 * Created by Fcy on 2015/3/11.
 */

/**
 * 热门职业与推荐职业使用相同的Adapter和item布局，
 */
public class HotJobAdapter extends BaseAdapter {

    private Context context;
    private List<Job> list;
    private LayoutInflater inflater;

    private int[] payTimesId = {R.drawable.rj, R.drawable.rj, R.drawable.zj, R.drawable.yj, R.drawable.wcj};
    private String[] jobTypes = {"", "派单", "服务员", "促销员", "礼仪", "客服", "小时工", "其他"};
    private String[] payUnits = {"", "元/小时", "元/天", "元/月", "元/件"};

    public HotJobAdapter(Context context, List<Job> list) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hot_job, parent, false);
            holder = new ViewHolder();
            holder.cropLog = (ImageView) convertView.findViewById(R.id.hot_cropLog);
            holder.corpName = (TextView) convertView.findViewById(R.id.hot_corp_name);
            holder.name = (TextView) convertView.findViewById(R.id.hot_name);
            holder.pay = (TextView) convertView.findViewById(R.id.hot_pay);
            holder.payUnit = (TextView) convertView.findViewById(R.id.hot_pay_unit);
            holder.jobType = (TextView) convertView.findViewById(R.id.hot_job_type);
            holder.jobSettleType = (ImageView) convertView.findViewById(R.id.hot_pay_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Job job = list.get(position);
        holder.corpName.setText(job.getCorpName());
        holder.name.setText(job.getName());
        holder.pay.setText(Integer.toString(job.getPay()));
        holder.payUnit.setText(payUnits[job.getPayUnit()]);
        holder.jobType.setText(jobTypes[job.getJobtypeId()]);
        holder.jobSettleType.setImageResource(payTimesId[job.getJobsettletypeId()]);
        // TODO 加载公司缩略图
        Log.i(">>", "公司缩略图地址:" + job.getCorpLogo());
        BitmapUtils bitmapUtils = BitmapUtilsHelper.initUtils(context);

        // 正在加载中的图片
        bitmapUtils.configDefaultLoadingImage(R.drawable.default_logo);
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_logo);

        bitmapUtils.display(holder.cropLog, job.getCorpLogo());

        return convertView;
    }

    private static class ViewHolder {
        ImageView cropLog;
        TextView corpName; // 公司名
        TextView name; // 招聘语
        TextView pay;
        TextView payUnit; // 元/每件
        TextView jobType; // 小时工
        ImageView jobSettleType; // 月结
    }
}
